package cn.middle.remote.serialization;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class TypeGenericity<T> implements ParameterizedType {
	
	
	private Type rawType;
	private Type[] argumentTypes;
	
	//是否带泛型
	private boolean genericityStatus = true;
	//原始类型
	private Class<?> originalClass;
	
	
	protected TypeGenericity(){
		Type superClass = getClass().getGenericSuperclass();

		if (superClass instanceof Class<?>) { // sanity check, should never happen
			throw new IllegalArgumentException("init error:  without actual genericity type information");
		}
		Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
		if (type instanceof ParameterizedType) { //带泛型
			ParameterizedType parameterizedType = (ParameterizedType) type;
			this.rawType = parameterizedType.getRawType();
			this.argumentTypes = parameterizedType.getActualTypeArguments();
		}else {
			if(type instanceof Class<?>) {
				originalClass = (Class<?>)type;
				genericityStatus = false;
			}
			else {
				throw new IllegalArgumentException("init error:  without actual type information");
			}

		}
	}

	public Class<?> getOriginalClass() {
		return originalClass;
	}

	public boolean isGenericity() {
		return genericityStatus;
	}
	
	@Override
	public Type[] getActualTypeArguments() {
		return argumentTypes;
	}

	@Override
	public Type getRawType() {
		return rawType;
	}

	@Override
	public Type getOwnerType() {
		return null;
	}



}
